/**
 * 设备状态统计
 */

import { trendAlarm } from "@/api/modules/iot/alarm";
import { productTotalStat } from "@/api/modules/stat/stat";
interface Count {
	deviceTotal: {
		alarmTotal: number;
		total: number;
		online: number;
		offline: number;
	};
	deviceTypeTotal: {
		[key: string]: any;
	};
}
export const useDeviceTotal = () => {
	const countData = reactive<Count>({
		deviceTotal: {
			alarmTotal: 0,
			total: 0,
			online: 0,
			offline: 0
		},
		deviceTypeTotal: {}
	});
	const getDeviceCount = async () => {
		let { result } = await productTotalStat();
		result = result || [];
		result.forEach((item: any) => {
			countData.deviceTotal.online += item.online;
			countData.deviceTotal.offline += item.offline;
			countData.deviceTypeTotal[item.productModel] = {
				...item,
				total: item.online + item.offline
			};
			countData.deviceTotal.total = countData.deviceTotal.online + countData.deviceTotal.offline;
		});
	};

	const getDeviceAlarmCount = async () => {
		let { result } = await trendAlarm({});
		console.log(result);
	};
	return { getDeviceCount, getDeviceAlarmCount, ...toRefs(countData) };
};
